<template>
  <div>
    <el-tree ref="tree" node-key="id" :data="list" :props="{label:'name'}" show-checkbox check-strictly default-expand-all />
    <el-row type="flex">
      <el-button type="primary" @click="hSubmit">确认</el-button>
      <el-button type="primary">取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permissions'
import { transDataToTreeData } from '@/utils/index'
import { getRolesId, assignPrem } from '@/api/setting'
export default {
  props: {
    cuId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadAssignPermission()
  },
  methods: {
    // 点击分配权限 获取权限列表
    async loadAssignPermission() {
      const res = await getPermissionList()
      console.log(res, 'getPermissionList')
      // 被勾选的复选框  setCheckedKeys 设置目前勾选的(res1.data.permIds)节点
      const res1 = await getRolesId(this.cuId)
      console.log(res1, 'getRolesId222')
      this.$refs.tree.setCheckedKeys(res1.data.permIds)
      this.list = transDataToTreeData(res.data)
    },
    // 点击确认 增加或删除选中的权限
    hSubmit() {
      // this.$refs.tree.getCheckedKeys() 返回所有show-checkbox选中的节点组成的数组
      const permIds = this.$refs.tree.getCheckedKeys()
      assignPrem({ id: this.cuId, permIds: permIds })
      this.$emit('success')
    }
  }
}
</script>
